
package com.rt.schedulenew.web;

import com.rt.schedulebase.dto.ExamApplyDto;
import com.rt.schedulebase.dto.ExamApplyItemDto;
import com.rt.schedulebase.entity.ExamRefer;
import com.rt.schedulenew.api.IExamApplyPatientService;
import com.rt.schedulenew.api.IExamApplyService;
import com.rt.schedulenew.api.IExamReferService;
import com.rt.schedulenew.service.impl.ConfigServiceImpl;
import com.rt.schedulenew.utils.base.CommonUtil;
import com.rt.schedulenew.utils.base.ConstantsUtil;
import com.rt.schedulenew.utils.checkparam.config.Check;
import com.rt.schedulenew.utils.global.JsonResult;
import com.rt.schedulenew.utils.global.LocalLock;
import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;
import java.util.Map;

@RestController
@RequestMapping({"/schedule"})
public class ScheduleOutController {
    private Logger log = LoggerFactory.getLogger(ScheduleOutController.class);
    @Autowired
    private IExamApplyPatientService examApplyPatientService;
    @Autowired
    private IExamApplyService examApplyService;
    @Autowired
    private ConfigServiceImpl configServiceImpl;
    @Autowired
    private IExamReferService examReferService;

    @PostMapping({"/date/list/out"})
    public JsonResult getDateListOut(@RequestBody ExamApplyDto dto) {
        JsonResult jsonResult = examApplyService.getDateList(dto);
        return jsonResult;
    }

    @PostMapping({"/apm/list/out"})
    public JsonResult getApmListOut(@RequestBody ExamApplyDto dto) {
        JsonResult jsonResult = examApplyService.getApmList(dto);
        return jsonResult;
    }

    @PostMapping({"/apm/confirm/out"})
    @LocalLock(key = "localLock:arg[0]")
    @Check({"itemCode"})
    public JsonResult confirmApmOut(@RequestBody ExamApplyDto dto) {
        JsonResult jsonResult = examApplyService.setApm(dto);
        if (jsonResult.getStatus().equals("0")) {
            Map<String, String> map = (Map<String, String>) jsonResult.getResult();
            ExamApplyDto examApplyDto = new ExamApplyDto();
            List<String> applyNos = CommonUtil.mergeNo2ApplyNos(map.get("mergeNo"));
            examApplyDto.setApplyNos(applyNos);
            String actType = ConstantsUtil.ActType.CONFIRM.getType();
            examApplyDto.setActType(actType);
            examApplyDto.setExamClass(map.get("examClass"));
            examApplyDto.setScheduledId(map.get("scheduledId"));
            examApplyDto.setMergeNo(map.get("mergeNo"));
            examApplyDto.setApplyNo(map.get("applyNo"));
            configServiceImpl.isScheduleSync(examApplyDto, actType, ConstantsUtil.ScheduleStatus.EIGHT.getStatus());
        }
        return jsonResult;
    }

    @PostMapping({"/apm/del/out"})
    public JsonResult delApmOut(@RequestBody ExamApplyDto dto) {
        JsonResult jsonResult = examApplyService.delApm(dto);
        if (jsonResult.getStatus().equals("0")) {
            ExamApplyDto applyResult = (ExamApplyDto) jsonResult.getResult();
            ExamApplyDto examApplyDto = new ExamApplyDto();
            List<String> applyNos = CommonUtil.mergeNo2ApplyNos(applyResult.getMergeNo());
            examApplyDto.setApplyNos(applyNos);
            String actType = ConstantsUtil.ActType.CANCEL.getType();
            examApplyDto.setActType(actType);
            examApplyDto.setExamClass(applyResult.getExamClass());
            examApplyDto.setScheduledId(applyResult.getScheduledId());
            examApplyDto.setMergeNo(applyResult.getMergeNo());
            examApplyDto.setApplyNo(dto.getApplyNo());
            examApplyDto.setTerminalType(dto.getTerminalType());
            examApplyDto.setScheduleDate(applyResult.getScheduleDate());
            examApplyDto.setName(applyResult.getName());
            examApplyDto.setSex(applyResult.getSex());
            examApplyDto.setAge(applyResult.getAge());
            examApplyDto.setScheduleApm(applyResult.getScheduleApm());
            examApplyDto.setScheduleHospitalName(applyResult.getScheduleHospitalName());
            examApplyDto.setPerformDeptName(applyResult.getPerformDeptName());
            String itemNameStr = "";
            for (ExamApplyItemDto item : applyResult.getItems()) {
                itemNameStr = itemNameStr + item.getItemName() + ";";
            }
            examApplyDto.setItemNameStr(itemNameStr);
            configServiceImpl.isScheduleSync(examApplyDto, actType, ConstantsUtil.ScheduleStatus.NINE.getStatus());
        }
        return jsonResult;
    }

    @PostMapping({"/apm/edit/out"})
    public JsonResult editApmOut(@RequestBody ExamApplyDto dto) {
        JsonResult jsonResult = examApplyService.editApm(dto);
        if (jsonResult.getStatus().equals("0")) {
            ExamApplyDto applyResult = (ExamApplyDto) jsonResult.getResult();
            ExamApplyDto examApplyDto = new ExamApplyDto();
            List<String> applyNos = CommonUtil.mergeNo2ApplyNos(applyResult.getMergeNo());
            examApplyDto.setApplyNos(applyNos);
            String actType = ConstantsUtil.ActType.UPDATE.getType();
            examApplyDto.setActType(actType);
            examApplyDto.setExamClass(applyResult.getExamClass());
            examApplyDto.setScheduledId(applyResult.getScheduledId());
            examApplyDto.setMergeNo(applyResult.getMergeNo());
            examApplyDto.setApplyNo(dto.getApplyNo());
            configServiceImpl.isScheduleSync(examApplyDto, actType, ConstantsUtil.ScheduleStatus.EIGHT.getStatus());
        }
        return jsonResult;
    }

    @PostMapping({"/apm/noRegular/out"})
    @LocalLock(key = "localLock:arg[0]")
    public JsonResult noRegularScheduleOut(@RequestBody ExamApplyDto dto) {
        JsonResult jsonResult = examApplyService.noRegularSchedule(dto);
        if (jsonResult.getStatus().equals("0")) {
            ExamApplyDto examApplyDto = new ExamApplyDto();
            String actType = ConstantsUtil.ActType.NO_REGULAR.getType();
            examApplyDto.setActType(actType);
            ExamApplyDto applyResult = (ExamApplyDto) jsonResult.getResult();
            examApplyDto.setExamClass(applyResult.getExamClass());
            examApplyDto.setScheduledId(applyResult.getScheduledId());
            examApplyDto.setMergeNo(applyResult.getMergeNo());
            examApplyDto.setApplyNo(applyResult.getApplyNo());
            examApplyDto.setApplyNos(CommonUtil.mergeNo2ApplyNos(applyResult.getMergeNo()));
            configServiceImpl.isScheduleSync(examApplyDto, actType, ConstantsUtil.ScheduleStatus.EIGHT.getStatus());
        }
        return jsonResult;
    }

    @PostMapping({"/getScheduleQueues/out"})
    public JsonResult getScheduleQueuesOut(@RequestBody ExamApplyDto dto) {
        return examApplyService.getScheduleQueues(dto);
    }

    @PostMapping({"/getExamReferSchedules"})
    public JsonResult getExamReferSchedules(@RequestBody ExamApplyDto dto) {
        return examReferService.getExamReferSchedules(dto);
    }

    @RequestMapping({"/matchRealExamApply"})
    public JsonResult matchRealExamApply(@RequestBody ExamRefer dto) {
        return examReferService.matchRealExamApply(dto);
    }

    @RequestMapping({"/cancelMatchExamApply"})
    public JsonResult cancelMatchExamApply(@RequestBody ExamRefer dto) {
        return examReferService.cancelMatchExamApply(dto);
    }

    @PostMapping({"/apm/sign/out"})
    @LocalLock(key = "localLock:arg[0]")
    public JsonResult signinSchedule(@RequestBody ExamApplyDto dto) {
        JsonResult jsonResult = examApplyService.signinSchedule(dto);
        if (jsonResult.getStatus().equals("0")) {
            ExamApplyDto ea = (ExamApplyDto) jsonResult.getResult();
            if (StringUtils.isBlank(ea.getIsSign())) {
                ExamApplyDto examApplyDto = new ExamApplyDto();
                List<String> applyNos = CommonUtil.mergeNo2ApplyNos(dto.getMergeNo());
                examApplyDto.setApplyNos(applyNos);
                String actType = ConstantsUtil.ActType.SIGN.getType();
                examApplyDto.setActType(actType);
                ExamApplyDto applyResult = (ExamApplyDto) jsonResult.getResult();
                examApplyDto.setExamClass(applyResult.getExamClass());
                examApplyDto.setScheduledId(applyResult.getScheduledId());
                examApplyDto.setMergeNo(applyResult.getMergeNo());
                examApplyDto.setApplyNo(dto.getApplyNo());
                configServiceImpl.isScheduleSync(examApplyDto, actType, ConstantsUtil.ScheduleStatus.TEN.getStatus());
            }
        }
        return jsonResult;
    }

    /**
     * 取消报到
     *
     * @param dto
     * @return
     */
    @PostMapping({"/apm/cancelSign/out"})
    @LocalLock(key = "localLock:arg[0]")
    public JsonResult cancelSign(@RequestBody ExamApplyDto dto) {
        JsonResult jsonResult = examApplyService.delSign(dto);
        if (jsonResult.getStatus().equals("0")) {
            ExamApplyDto examApplyDto = new ExamApplyDto();
            List<String> applyNos = CommonUtil.mergeNo2ApplyNos(dto.getMergeNo());
            examApplyDto.setApplyNos(applyNos);
            String actType = ConstantsUtil.ActType.CANCEL_SIGN.getType();
            examApplyDto.setActType(actType);
            ExamApplyDto applyResult = (ExamApplyDto) jsonResult.getResult();
            examApplyDto.setExamClass(applyResult.getExamClass());
            examApplyDto.setScheduledId(applyResult.getScheduledId());
            examApplyDto.setMergeNo(applyResult.getMergeNo());
            examApplyDto.setApplyNo(dto.getApplyNo());
            examApplyDto.setTerminalType(dto.getTerminalType());
            examApplyDto.setScheduleDate(applyResult.getScheduleDate());
            examApplyDto.setName(applyResult.getName());
            examApplyDto.setSex(applyResult.getSex());
            examApplyDto.setAge(applyResult.getAge());
            examApplyDto.setScheduleApm(applyResult.getScheduleApm());
            examApplyDto.setScheduleHospitalName(applyResult.getScheduleHospitalName());
            examApplyDto.setPerformDeptName(applyResult.getPerformDeptName());
            String itemNameStr = "";
            for (ExamApplyItemDto item : applyResult.getItems()) {
                itemNameStr = itemNameStr + item.getItemName() + ";";
            }
            examApplyDto.setItemNameStr(itemNameStr);
            configServiceImpl.isScheduleSync(examApplyDto, actType, ConstantsUtil.ScheduleStatus.ZEROZERO.getStatus());
        }
        return jsonResult;
    }


    @PostMapping({"/patient/list/out"})
    public JsonResult getPatientListOut(@RequestBody ExamApplyDto dto) {
        JsonResult jsonResult = examApplyPatientService.getPatientListOut(dto);
        return jsonResult;
    }

}
